package com.clg.common.tools;

import com.jfinal.kit.Ret;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by usercai on 2016/9/28.
 */
public class RetRes extends Ret{
    public static final String SUCCESS_FLAG = "success";
    public static final String MESSAGE_FLAG = "retmsg";

    private Map<Object, Object> data = new HashMap<Object, Object>();

    public RetRes() {
        put(SUCCESS_FLAG,false);
    }

    public boolean isSuccess(){
        return get(SUCCESS_FLAG);
    }

    public RetRes setSuccess(boolean success){
        return put(SUCCESS_FLAG,success);
    }

    public RetRes setMessage(String message){
        return put(MESSAGE_FLAG,message);
    }

    public String getMessage(){
        return get(MESSAGE_FLAG);
    }

    public static RetRes create() {
        return new RetRes().put(SUCCESS_FLAG,false);
    }

    public static RetRes create(boolean success) {
        return new RetRes().put(SUCCESS_FLAG,success);
    }

    public static RetRes create(boolean success,String msg) {
        return new RetRes().put(SUCCESS_FLAG,success).put(MESSAGE_FLAG,msg);
    }

    public RetRes put(Object key, Object value) {
        data.put(key, value);
        return this;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public RetRes put(Map map) {
        this.data.putAll(map);
        return this;
    }

    public RetRes put(RetRes ret) {
        this.data.putAll(ret.data);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T> T get(Object key) {
        return (T)data.get(key);
    }

    public boolean isEmpty() {
        return data.isEmpty();
    }

    public int size() {
        return data.size();
    }

    public RetRes clear() {
        data.clear();
        return this;
    }

    public boolean equals(RetRes ret) {
        return ret != null && this.data.equals(ret.data);
    }

    /**
     * key 存在，但 value 可能为 null
     */
    public boolean containsKey(Object key) {
        return data.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return data.containsValue(value);
    }

    /**
     * key 存在，并且 value 不为 null
     */
    public boolean notNull(Object key) {
        return data.get(key) != null;
    }

    /**
     * key 不存在，或者 key 存在但 value 为null
     */
    public boolean isNull(Object key) {
        return data.get(key) == null;
    }

    /**
     * key 存在，并且 value 为 true，则返回 true
     */
    public boolean isTrue(Object key) {
        Object value = data.get(key);
        return (value instanceof Boolean && ((Boolean)value == true));
    }

    /**
     * key 存在，并且 value 为 false，则返回 true
     */
    public boolean isFalse(Object key) {
        Object value = data.get(key);
        return (value instanceof Boolean && ((Boolean)value == false));
    }

    @SuppressWarnings("unchecked")
    public <T> T remove(Object key) {
        return (T)data.remove(key);
    }

    public Map<Object, Object> getData() {
        return data;
    }
}
